﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class ChangeInformationApplicant : System.Web.UI.Page
{
    string UrlPath = "";
    ApplicantMethord AM = new ApplicantMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            UrlPath = Request.RawUrl.Substring(Request.RawUrl.LastIndexOf("/") + 1, Request.AppRelativeCurrentExecutionFilePath.Length - 2);
            if(Request.QueryString["ID"].ToString() != "")
            {
                UrlPath += "?ID=" + Request.QueryString["ID"].ToString();
            }
        }
        catch (Exception ex)
        {
        }
        txtDate_CalendarExtender.EndDate = DateTime.Now;
        try
        {
            Session["PageCurrent"] = UrlPath;

            if (Session["User"] != null)
            {
                txtUser.Text = Session["User"].ToString();
            }
            else
            {
                Response.Redirect("LoginApplicant.aspx");
            }
            if (!IsPostBack)
            {
                LoadData(txtUser.Text);
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("LoginApplicant.aspx");
        }
    }
    private void LoadData(string _User)
    {
        try
        {
            SqlDataReader dr = AM.ReturnInformationApplicantFollowID(_User);
            while (dr.Read())
            {
                txtName.Text = dr.GetString(1);
                txtAddress.Text = dr.GetString(2);
                txtPhone.Text = dr.GetString(3);
                txtDate.Text = dr.GetDateTime(4).ToShortDateString();
                txtEmail.Text = dr.GetString(5);
                txtEducation.Text = dr.GetString(6);
                txtLanguages.Text = dr.GetString(7);
                txtWork.Text = dr.GetString(8);
            }
            AM.connClose();
        }
        catch (Exception ex)
        {
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string mathEmail = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        string mathPhone = @"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$";
        lblError.Text = "";
        if (txtName.Text.Equals(""))
        {
            lblError.Text = "FullName can not be empty.";
        }
        else if (txtAddress.Text.Equals(""))
        {
            lblError.Text = "Address can not be empty.";
        }
        else if (txtPhone.Text.Equals(""))
        {
            lblError.Text = "Phone can not be empty.";
        }
        else if (!Regex.IsMatch(txtPhone.Text, mathPhone))
        {
            lblError.Text = "Phone Invalid.";
        }
        else if (txtDate.Text.Equals(""))
        {
            lblError.Text = "Date of birth can not be empty.";
        }
        else if (txtEmail.Text.Equals(""))
        {
            lblError.Text = "Email can not be empty.";
        }
        else if (!Regex.IsMatch(txtEmail.Text, mathEmail))
        {
            lblError.Text = "Email Invalid.";
        }
        else
        {
            try
            {
                Convert.ToDateTime(txtDate.Text);
                bool Result =  AM.ChangeApplicant(txtUser.Text, txtName.Text, txtAddress.Text, txtPhone.Text, txtDate.Text, txtEmail.Text, txtEducation.Text, txtLanguages.Text, txtWork.Text);
                if (Result)
                {
                    Response.Write("<script type='text/javascript'>alert('Create new Applicant Successful!'); window.location = 'Home.aspx'</script>");
                }
                else
                {
                    lblError.Text = "Unsuccessful";
                }
            }
            catch (Exception ex)
            {
                lblError.Text = "Date of birth Invalid.";
            }
        }
    }
}